﻿using EasyNet.Componets.Core.Serialization;
using ServiceStack;
using ServiceStack.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Json
{
    public class ServiceStackTextJson : IJsonSerialization
    {
        public ServiceStackTextJson()
        {
            JsConfig.ThrowOnDeserializationError = true;
            
        }

        public string Serialize(object obj)
        {
            JsConfig.DateHandler = DateHandler.ISO8601;
            return obj.ToJson();
        }

        public void Serialize(object value, System.Type type, System.IO.Stream writeStream)
        {
            JsConfig.DateHandler = DateHandler.ISO8601;
            JsonSerializer.SerializeToStream(value, type, writeStream);
        }
        
        public T Deserialize<T>(string value)
        {
            return value.FromJson<T>();
        }

        public object Deserialize(System.Type type, System.IO.Stream stream)
        {
            return JsonSerializer.DeserializeFromStream(type, stream);
        }

        public object Deserialize(string value, Type type)
        {
            return JsonSerializer.DeserializeFromString(value, type);
        }

     
    }
}
